//
//  FLLMatchViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/3.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLMatchViewController.h"
#import "ZWYNetTool.h"
#import "FLLMatchModel.h"
#import "MatchData.h"
#import "MatchSecData.h"
#import "MatchThirdData.h"
#import "FLLMacro.h"
#import "FLLMatchCollectionViewCell.h"
#import "Masonry.h"
#import "UILabel+FLLSetPropety.h"
#import "UIImageView+WebCache.h"
#import "UIView+Frame.h"
@interface FLLMatchViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) FLLMatchModel *fllMatchModel;
@property (nonatomic, strong) UICollectionView *mainCollectionView;
/* collectionView数据源 */
@property (nonatomic, strong) NSArray<MatchThirdData *> *collectionArray;
/* 最底部的view */
@property (nonatomic, strong) UIView *choseView;
@property (nonatomic, strong) UILabel *choseLabel;
@property (nonatomic, strong) UIView *blackView;
@property (nonatomic, strong) UIView *moveView;
@property (nonatomic, strong) UITableView *choseTableView;
@property (nonatomic, strong) UIButton *arrowButton;
@property (nonatomic, assign) BOOL isShow;
/* tableView数据源 */
@property (nonatomic, strong) NSArray<MatchSecData *> *tableArray;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, assign) NSInteger selectedNumber;
@property (nonatomic, strong) NSMutableArray *numberArray;
@property (nonatomic, assign) BOOL isSelected;
@property (nonatomic, strong) NSMutableArray *selectedArray;
@property (nonatomic, strong) NSMutableArray *indexArray;
@property (nonatomic, strong) NSMutableArray *rightNumberArray;
@property (nonatomic, strong) NSMutableArray *oneArray;
@property (nonatomic, strong) NSMutableArray *twoArray;
@end

@implementation FLLMatchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.selectedNumber = -1;
    self.numberArray = [NSMutableArray array];
    self.selectedArray = [NSMutableArray array];
    self.indexArray = [NSMutableArray array];
    self.rightNumberArray = [NSMutableArray array];
    self.oneArray = [NSMutableArray array];
    self.twoArray = [NSMutableArray array];
    [self createPOSTRequest];
    [self createMainCollectionView];
    [self createChoseView];
    [self createMoveView];
    [self createChoseTableView];
    [self createArrowButton];
    
}

- (void)createPOSTRequest
{
    
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1"
andBody:@"methodName=MaterialSubtype&version=4.40&" andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
    
    self.fllMatchModel = [FLLMatchModel modelWithDic:result];
    self.tableArray = self.fllMatchModel.data.data;
    _currentIndex = 0;
    self.collectionArray = self.fllMatchModel.data.data[_currentIndex].data;
    [self.mainCollectionView reloadData];
    [self.choseTableView reloadData];
    } andFailureBlock:^(NSError *error) {
    
}];


}
- (void)createMainCollectionView
{

    UICollectionViewFlowLayout *mainLayout = [[UICollectionViewFlowLayout alloc] init];
    mainLayout.minimumLineSpacing = CellMargin_10;
    mainLayout.minimumInteritemSpacing = CellMargin_30;
    mainLayout.sectionInset = UIEdgeInsetsMake(CellMargin_30, CellMargin_30, CellMargin_30, CellMargin_30);
    
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64) collectionViewLayout:mainLayout];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    self.mainCollectionView.backgroundColor = [UIColor whiteColor];
    [self.mainCollectionView registerNib:[UINib nibWithNibName:@"FLLMatchCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"matchCollectionCell"];
    
    [self.view addSubview:_mainCollectionView];
    
}
- (void)createChoseView
{

    self.choseView = [[UIView alloc] init];
    [self.view addSubview:_choseView];
    [self.choseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_equalTo(49);
    }];
    
    UILabel *grayLabel = [[UILabel alloc] init];
    [_choseView addSubview:grayLabel];
    [grayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo (0);
        make.height.mas_equalTo(1);
    }];
    grayLabel.backgroundColor = FLLLightGrayColor;
    self.choseLabel = [[UILabel alloc] init];
    [self.choseView addSubview:_choseLabel];
    [self.choseLabel setPropetyWithTextColor:[UIColor blackColor] Alignment:1 textFont:[UIFont systemFontOfSize:15 * kWidthScale]];
    self.choseLabel.numberOfLines = 0;
    self.choseLabel.text = @"选择2个或3个食材\n掌厨告诉你可以做那些菜";
    [self.choseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(grayLabel.mas_bottom);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(_choseView.mas_width).multipliedBy(0.5);
        make.centerX.mas_equalTo(0);
    }];
    
    
}
- (void)createBlackView
{

    self.blackView = [[UIView alloc] init];
    [self.view addSubview:_blackView];
    [self.blackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-49);
    }];
    self.blackView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.53];

}
- (void)createMoveView
{

    self.moveView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH / 2.0, SCREEN_HEIGHT - 49 - 64)];
    self.moveView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_moveView];
    
}
- (void)createChoseTableView
{

    self.choseTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, _moveView.fll_width - 20 * kWidthScale, _moveView.fll_height) style:UITableViewStylePlain];
    [self.moveView addSubview:_choseTableView];
    self.choseTableView.delegate = self;
    self.choseTableView.dataSource = self;
    self.choseTableView.showsVerticalScrollIndicator = NO;
    [self.choseTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"choseTableCell"];
    
}
- (void)createArrowButton
{

    self.arrowButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:_arrowButton];
    self.arrowButton.frame = CGRectMake(SCREEN_WIDTH - 30 * kWidthScale, ((SCREEN_HEIGHT - 49) / 2.0 - 30) * kHeightScale, 30 * kWidthScale, 60 * kHeightScale);
    [self.arrowButton setImage:[UIImage imageNamed:@"ArrowLeft"] forState:UIControlStateNormal];
    self.arrowButton.backgroundColor = [UIColor whiteColor];
    self.arrowButton.layer.borderWidth = 1;
    self.arrowButton.layer.borderColor = [UIColor colorWithWhite:0.712 alpha:1.000].CGColor;
    self.arrowButton.layer.masksToBounds = YES;
    self.arrowButton.layer.cornerRadius = 2.0f;
    [self.arrowButton addTarget:self action:@selector(didClickedArrowButton:) forControlEvents:UIControlEventTouchUpInside];
    
}
// button 点击事件
- (void)didClickedArrowButton:(UIButton *)button
{

    if (_isShow == YES) {
        [UIView animateWithDuration:0.2 animations:^{
            self.moveView.fll_x = SCREEN_WIDTH;
            self.arrowButton.fll_x = SCREEN_WIDTH - 30 * kWidthScale;
            [self.arrowButton setImage:[UIImage imageNamed:@"ArrowLeft"] forState:UIControlStateNormal];
        } completion:^(BOOL finished) {
            _isShow = NO;
            [self.blackView removeFromSuperview];
        }];

    }
    // 没显示View
    if (_isShow == NO) {
    [UIView animateWithDuration:0.2 animations:^{
        self.moveView.fll_x = SCREEN_WIDTH / 2.0;
        self.arrowButton.fll_x = SCREEN_WIDTH / 2.0 - 30 * kWidthScale;
        [self.arrowButton setImage:[UIImage imageNamed:@"ArrowRight"] forState:UIControlStateNormal];
        [self createBlackView];
        
    } completion:^(BOOL finished) {
        _isShow = YES;
        [self.view bringSubviewToFront:_moveView];
        [self.view bringSubviewToFront:_arrowButton];
       
       
    }];
    }
}
#pragma mark -- TableView DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _tableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"choseTableCell"];
    // 取消点击cell变灰效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = _tableArray[indexPath.row].text;
    cell.textLabel.font = [UIFont systemFontOfSize:15 * kWidthScale];
    if (indexPath.row == _currentIndex) {
        cell.textLabel.textColor = FLLOrangeColor;
    } else {
    
        cell.textLabel.textColor = [UIColor lightGrayColor];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    
    NSLog(@"-----------%zd", _oneArray.count);//???????????为什么是空
    
        
    if (_numberArray.count != 0) {
        self.oneArray = _numberArray;
        self.twoArray = _selectedArray;
        
        NSLog(@"%zd***********", _oneArray.count);
    }

    [self.rightNumberArray addObject:[NSString stringWithFormat:@"%zd", 0]];
    self.currentIndex = indexPath.row;
    NSLog(@"第%zd个分区", _currentIndex);
    
//    [self.rightNumberArray addObject:[NSString stringWithFormat:@"%zd", _currentIndex]];
    
    self.collectionArray = _tableArray[_currentIndex].data;
    
    if (![self.rightNumberArray containsObject:[NSString stringWithFormat:@"%zd", _currentIndex]]) {
        [self.numberArray removeAllObjects];
        [self.selectedArray removeAllObjects];
    
    } else
    {
        
        self.numberArray = _oneArray;
        self.selectedArray = _twoArray;
        NSLog(@"%zd+++++++++++++", _oneArray.count);
    }
    
    
   
//    for (int i = 0; i < _indexArray.count; i ++) {
//                if (_currentIndex != [_indexArray[i] integerValue]) {
//           
//            
//        }
//    }
//    [self.indexArray addObject:[NSString stringWithFormat:@"%zd", _currentIndex]];
    

    
    [UIView animateWithDuration:0.2 animations:^{
        self.moveView.fll_x = SCREEN_WIDTH;
        self.arrowButton.fll_x = SCREEN_WIDTH - 30 * kWidthScale;
        [self.arrowButton setImage:[UIImage imageNamed:@"ArrowLeft"] forState:UIControlStateNormal];
    } completion:^(BOOL finished) {
        _isShow = NO;
        [self.blackView removeFromSuperview];
    }];
    
    
    
    
    
    
    
    
    [self.mainCollectionView reloadData];
    [self.choseTableView reloadData];

   
}
#pragma mark -- Collection DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return _collectionArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    FLLMatchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"matchCollectionCell" forIndexPath:indexPath];
    [cell.materialImageView sd_setImageWithURL:[NSURL URLWithString:_collectionArray[indexPath.row].image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    
    cell.selectedImageView.image = nil;
    for (int i = 0; i < _numberArray.count; i ++) {
        if (indexPath.row == [_numberArray[i] integerValue]) {
    
            cell.selectedImageView.image = [UIImage imageNamed:@"materialSelected"];
        }

    }
        cell.materialNameLabel.text = _collectionArray[indexPath.row].text;
    cell.materialNameLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
    
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CGFloat itemWidth = (_mainCollectionView.bounds.size.width - 5 * CellMargin_30 ) / 4.0;
    CGFloat itemHeight = itemWidth * 1.5;
    return CGSizeMake(itemWidth, itemHeight);
}
// 实现食材搭配的挑选
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    self.selectedNumber = indexPath.row;
    if (![_selectedArray containsObject:[NSString stringWithFormat:@"%zd", _selectedNumber]]) {
        [self.numberArray addObject:[NSString stringWithFormat:@"%zd", _selectedNumber]];
       [self.selectedArray addObject:[NSString stringWithFormat:@"%zd", _selectedNumber]];
       [self.mainCollectionView reloadData];
    } else {
        for (int i = 0; i < _numberArray.count; i++) {
            if (self.selectedNumber == [_numberArray[i] integerValue]) {
                [self.numberArray removeObjectAtIndex:i];
                [self.selectedArray removeObjectAtIndex:i];
            }
        }
        
        [self.mainCollectionView reloadData];
    }
    
   
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
